/*
 * Created on 2006-2-10
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.hitrust.gs.action;



import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletResponse;

import com.hitrust.gs.business.GSProductTiptopStockProcesser;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.vo.GSAllWarehouseCodeVO;
import com.hitrust.gs.vo.GSProductCountVO;
import com.hitrust.gs.vo.WarehouseCodeVO;

/*
 * 订单录入 中产品编码验证 -
 */
public class GSOrderInputValidateAction extends GSBaseAction {
	
    public String execute(GSBaseForm aForm) throws Exception {
		String path = aForm.getRequest().getServletPath();
		if ("/A100112.do".equals(path)) {
			String deptId = aForm.getRequest().getParameter("deptid");
	    	GSProductTiptopStockProcesser ordProcesser = new GSProductTiptopStockProcesser();
	    	int isExist = ordProcesser.isExistRow("select product_code from t_warehouse_max_storage_config where product_code = '"+deptId+"'");

	    	HttpServletResponse response = aForm.getResponse();
	    	PrintWriter out = response.getWriter();
	    	out.println(isExist);
	    	out.close();

		}else if ("/A100113.do".equals(path)) {
			String deptid = aForm.getRequest().getParameter("deptid");
			
			String proCode = aForm.getRequest().getParameter("proCode");
			
			//用户输入的订货数量
			String proCount = aForm.getRequest().getParameter("proCount");
		
			//返回客户端的消息
		 	String returnMessage = null;
		 	
			//分仓编号与产品编号不能为空
			if (deptid != null && !"".equals(deptid) && proCode != null && !"".equals(proCode)) {
				
				//6编码分公司
				String wareHouseCode = deptid.substring(0,6);
			
				//用户输入的订货数量
				float proCountNum = 0;
				if (proCount != null) {
		 			proCountNum =  Float.parseFloat(proCount);
				}
		 		
				
			 	GSProductTiptopStockProcesser ordProcesser = new GSProductTiptopStockProcesser();
			 	//最大库存量
			 	ArrayList wareHouseCodeList = ordProcesser.selectWarehouseCode(proCode,wareHouseCode);
			 	//订货单位所属分公司已经在仓库维护页面启用了库存设置，在输入订货数量时会执行验证。
			 	int isExistWhsc = ordProcesser.isExistWarehouseStorageConfig(wareHouseCode);
			 	
			 	int maxCount =0;
			 	
			 	if (isExistWhsc !=0 && wareHouseCodeList != null && wareHouseCodeList.size() > 0) {
			 		
			 		WarehouseCodeVO vo =(WarehouseCodeVO)wareHouseCodeList.get(0);
			 		maxCount = Integer.parseInt(vo.getMax_count());			
			 
				 	//单个分公司库存量
				 	ArrayList productCountList = ordProcesser.selectProductCount(proCode,wareHouseCode);
				 	float productCounts =0;
				 	if (productCountList != null) {
					 	GSProductCountVO ProductCount = (GSProductCountVO)productCountList.get(0);
					    productCounts =  Float.parseFloat(ProductCount.getProductCount());
				 	}
				 	
				 	
				 	
				 	if (productCounts < maxCount) {
				 		float cha = maxCount - productCounts;
				 		
				 		
				 		//判断其输入的数字小于差额则不做修改，大于差额用差额替换
						if (proCountNum>cha) {
							returnMessage = String.valueOf(cha);
						}else{
							returnMessage = String.valueOf(proCountNum);
						}
					}else{
						//分公司下的所有分仓库存量(如果高于最高库存，验证不通过的提示)
						float num = productCounts - maxCount;
				
						StringBuffer sb = new StringBuffer();
						sb.append("高于最高库存"+num+"，最高库存为"+maxCount+"，分布为：");
					
						ArrayList allWarehouseList = ordProcesser.selectAllProductCount(proCode,wareHouseCode);
						for (int i = 0; i < allWarehouseList.size(); i++) {
							GSAllWarehouseCodeVO allVo = (GSAllWarehouseCodeVO)allWarehouseList.get(i);
							sb.append(allVo.getWarehouse_name()+"仓库数量"+allVo.getWarehouse_Count());
							if (i != allWarehouseList.size()-1) {
								sb.append("，");
							}else{
								sb.append("。");
							}
						}
						returnMessage = sb.toString();
						
					}
			 	}else{
			 		returnMessage = String.valueOf(proCountNum);
			 	}
		 	
			}else{
				returnMessage = String.valueOf("订货单位，集中编码不能为空！");
			}
			
		 	
		 	HttpServletResponse response = aForm.getResponse();
		 	response.setContentType("text/html;charset=GBK");
	    	PrintWriter out = response.getWriter();
	    	
	    	out.println(returnMessage);
	    	out.close();
		}
    	        
        return null;
    }
}
